<?php

namespace app\api\controller;

use think\Controller;
use app\common\model\Pjdomain as PjdomainModel;
use app\common\model\Wxurls as WxurlsModel;

class Templatejump extends Controller
{
    /*
     * 新版多域名联和跳转
     * */
    public function manyurls($id){
        $this->_id = $id;
        $wxurls = new WxurlsModel();
        //  根据id挑出域名后缀
        $url_postfix = $wxurls->query('SELECT postfix,pid FROM think_wxurls WHERE id =  ' . $id );
        $url_postfix = $url_postfix[0];
//        var_dump($url_postfix);
        if($url_postfix){
            // 选出随机的二跳域名domain
            $url_domain = $wxurls->query('SELECT domain, id  FROM think_wxurls WHERE `status` = 1 AND type = 2 AND pid = '.$url_postfix['pid'].' ORDER BY RAND() LIMIT 1');
            if($url_domain){
                $wxurls->query('UPDATE think_wxurls set jump_reads=jump_reads+1 WHERE id =' . $url_domain[0]['id']);
                // 增加一次的访问量  update student set score=score+1 where id = 1
                $wxurls->query('UPDATE think_wxurls set jump_reads=jump_reads+1 WHERE id = ' . $id);
                $url_domain = $url_domain[0]['domain'];
                $domain =  $url_domain.'/'.$url_postfix['postfix'] .'/index.html' ;
                return json(['status' => 1, 'url' => $domain]);
            }else{
                // 域名被封完了, 就直接返回后缀, 接抗封老域名
                return json(['status' => 2, 'url' => $url_postfix['postfix'].'/index.html']);
            }
        }else{
            // 找不到对应的id
            return json(['status' => 3, 'msg' => 'ID is not here!']);
        }
    }

    /*
     * 新版多域名联和跳转 1.0
     * */
    public function manyurls1($id){
        $wxurls = new WxurlsModel();
        //  根据id挑出域名后缀
        $url_postfix = $wxurls->query('SELECT postfix FROM think_wxurls WHERE id =  ' . $id );
        $url_postfix = $url_postfix[0]['postfix'];
//        var_dump($url_postfix);
        if($url_postfix){
            // 选出随机的二跳域名domain
            $url_domain = $wxurls->query('SELECT domain, id  FROM think_wxurls WHERE `status` = 1 AND type = 2 ORDER BY RAND() LIMIT 1');
            if($url_domain){
                $wxurls->query('UPDATE think_wxurls set jump_reads=jump_reads+1 WHERE id =' . $url_domain[0]['id']);
                // 增加一次的访问量  update student set score=score+1 where id = 1
                $wxurls->query('UPDATE think_wxurls set jump_reads=jump_reads+1 WHERE id = ' . $id);
                $url_domain = $url_domain[0]['domain'];
                $domain =  $url_domain.'/'.$url_postfix;
                return json(['status' => 1, 'url' => $domain]);
            }else{
                // 域名被封完了, 就直接返回后缀, 接抗封老域名
                return json(['status' => 2, 'url' => $url_postfix]);
            }
        }else{
            // 找不到对应的id
            return json(['status' => 3, 'msg' => 'ID is not here!']);
        }
    }

    /**
     * http://tp5.adcount.sn/api/Templatejump/test/id/70/status/1
     * https://jmyf.downing.js.cn/.
        Accept: application/json, text/plain,
        Accept-Encoding: gzip, deflate, br
        Accept-Language: zh-CN,zh;q=0.9
        Connection: keep-alive
        Content-Length: 0
        Content-Type: application/json;charset=UTF-8
        Cookie: wxuin=2154502842; mm_lang=zh_CN; webwxuvid=3d96f0194a8dd4ed0bb856c1bc2bbc7ff525ee063aedb58480c2617d3a22da6c5f6d5fbf22c29fe0f651a8ca0ac00d86; MM_WX_NOTIFY_STATE=1; MM_WX_SOUND_STATE=1; wxpluginkey=1536101882; wxsid=J2eH1RDzpTzeTtq8; webwx_data_ticket=gSeaBSZ9BZ4q8/ihX89g+2nD; webwx_auth_ticket=CIsBEKOvobkGGoABJ7Hqf7N+wbC99a15k7da4MTwVRPOJFqQuSzi9yNuurw/bNTk6spzL5IaDGueCf2rg28iZm2mVUcDZQ0oMWRY79HGgqvjZ5gkPlnabyzPIwvvvEUwfaEqqSuWfzoo6K9yEwZ9FfNvKTBU+Y0fGH4AhnouRVxqXLtVQ1bES4LEg4Y=; login_frequency=1; last_wxuin=2154502842; wxloadtime=1536114075_expired
        Host: wx2.qq.com
        Origin: https://wx2.qq.com
        Referer: https://wx2.qq.com/?&lang=zh_CN
        User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
     */
    public function test($id)
    {
        $pjdomain_model = new PjdomainModel();
        $domain_lists = $pjdomain_model->where('pid', $id)->where('status', 1)->select();
        foreach ($domain_lists as $domain_list) {
            $url = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxcheckurl?requrl=https%3A%2F%2F'.$domain_list['domain'].$domain_list['postfix'].'&skey=%40crypt_707b846_586e6864a58b752442bb0fac6f8d03a3&deviceid=e146870155315770&pass_ticket=undefined&opcode=2&scene=1&username=@a2672871747ef95e77eda67e64acf2614574582207207a7cfcce57e8288342ef';
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_HTTPGET, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $cookie = 'pgv_pvi=7363110912; RK=oOwB1gqw7/; tvfe_boss_uuid=1606ac0fae971cdb; pac_uid=1_1106678464; uid=848493667; ptui_loginuin=1106678464; pt2gguin=o1106678464; luin=o1106678464; lskey=0001000067125a953c0ea0fe59063479290bcd0b3fefaeb2e9fb34446d1f5e26a5b52b8290dc73afd86cb088; ptcz=049f9c758a08f1f251e4ff9c36627a101925d7e0eecf42c2d4e0cd58f2673829; pgv_pvid=367289993; o_cookie=1106678464; MM_WX_NOTIFY_STATE=1; MM_WX_SOUND_STATE=1; mm_lang=zh_CN; webwxuvid=d51845e03ee6192b60529263034eef8d6018965a10d8317235e9f73c47d3bce60d63bd194fea5f41be35f4dce93d4ba2; webwx_auth_ticket=CIsBEMiXt4EGGoAB+ERupKGOfE7ixF2Uyx4qBV0BUqpsH4wO9s5iKLmNfbVqMD/WexF2C3PBdTXcPl2+YUdj/9CVsaZWiF7lOoCrL8iJI96FlPOSwyEQUjsSitj0sx6SIz/GWjBlwm5Ncb4JxT1a7gePNtNrAPZYV4EDzzr6/aWyqMapoJXy72L8d+0=; login_frequency=1; last_wxuin=719293304; wxloadtime=1536800979_expired; wxpluginkey=1536793802; wxuin=719293304; wxsid=ZxCYocFfMfjv9HVm; webwx_data_ticket=gSevzn7SfyFmnjZpBE54BImF';
            $user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36';
            curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
            curl_setopt($ch, CURLOPT_COOKIE, $cookie);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLINFO_HEADER_OUT, true);
            $output = curl_exec($ch);
            // $request_header = curl_getinfo($ch,CURLINFO_HEADER_OUT);
            curl_close($ch);

            // dump($request_header);
            $output_arr = json_decode($output, true);
            if (strpos($output_arr['FullURL'], 'weixin110.qq.com')) {
                continue;
            } else {
                // header('HTTP/1.1 301 Moved Permanently');//发出301头部
                // header("Location:https://".$domain_list['domain'].$domain_list['postfix']);//跳转到你希望的地址格式
                return json(['status' => 1, 'url' => $domain_list['domain'].'/'.$domain_list['postfix']]);
            }
        }

        return json(['status' => 3, 'msg' => 'Unusable domain name']);
    }

    /**
     * http://tp5.adcount.sn/api/Templatejump/jumptourl/id/55.
     */
    public function jumptourl($id)
    {
        $pjdomain_model = new PjdomainModel();
        $domain_list = $pjdomain_model->where('pid', $id)->where('status', 1)->find();
        header('HTTP/1.1 301 Moved Permanently'); //发出301头部
        header('Location:https://'.$domain_list['domain']); //跳转到你希望的地址格式
    }

    public function jumptourl2($id)
    {
        $pjdomain_model = new PjdomainModel();
        $domain_list = $pjdomain_model->query('SELECT
        *
    FROM
        `think_pjdomain` WHERE `pid` = '.$id.' AND `status` = 1
    ORDER BY
        RAND()
    LIMIT 1;');
        if ($domain_list) {
            $domain_list = $domain_list[0];
            $domain = $domain_list['domain'].'/'.$domain_list['postfix'].'/index.html';
            // httpRequest('http://check.yougou520.cn:9501/index/addtask', 'POST', ['domain' => $domain, 'id' => $domain_list['id']]);

            return json(['status' => 1, 'url' => $domain]);
        } else {
         $postfix =   $pjdomain_model->query('SELECT    *
    FROM
        `think_pjdomain` WHERE `pid` = '.$id.'  LIMIT 1;');

            return json(['status' => 3, 'url' => $postfix[0]['postfix'].'/index.html']);
        }
    }

    public function jumptourl3($id)
    {
        $pjdomain_model = new PjdomainModel();
        $domain_lists = $pjdomain_model->where('pid', $id)->where('status', 1)->select();
        foreach ($domain_lists as $domain_list) {
            $url = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxcheckurl?requrl=https%3A%2F%2F'.$domain_list['domain'].$domain_list['postfix'].'&skey=%40crypt_707b846_586e6864a58b752442bb0fac6f8d03a3&deviceid=e146870155315770&pass_ticket=undefined&opcode=2&scene=1&username=@a2672871747ef95e77eda67e64acf2614574582207207a7cfcce57e8288342ef';
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_HTTPGET, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $cookie = 'pgv_pvi=7363110912; RK=oOwB1gqw7/; tvfe_boss_uuid=1606ac0fae971cdb; pac_uid=1_1106678464; uid=848493667; ptui_loginuin=1106678464; pt2gguin=o1106678464; luin=o1106678464; lskey=0001000067125a953c0ea0fe59063479290bcd0b3fefaeb2e9fb34446d1f5e26a5b52b8290dc73afd86cb088; ptcz=049f9c758a08f1f251e4ff9c36627a101925d7e0eecf42c2d4e0cd58f2673829; pgv_pvid=367289993; o_cookie=1106678464; MM_WX_NOTIFY_STATE=1; MM_WX_SOUND_STATE=1; mm_lang=zh_CN; webwxuvid=d51845e03ee6192b60529263034eef8d6018965a10d8317235e9f73c47d3bce60d63bd194fea5f41be35f4dce93d4ba2; webwx_auth_ticket=CIsBEMiXt4EGGoAB+ERupKGOfE7ixF2Uyx4qBV0BUqpsH4wO9s5iKLmNfbVqMD/WexF2C3PBdTXcPl2+YUdj/9CVsaZWiF7lOoCrL8iJI96FlPOSwyEQUjsSitj0sx6SIz/GWjBlwm5Ncb4JxT1a7gePNtNrAPZYV4EDzzr6/aWyqMapoJXy72L8d+0=; login_frequency=1; last_wxuin=719293304; wxloadtime=1536800979_expired; wxpluginkey=1536793802; wxuin=719293304; wxsid=ZxCYocFfMfjv9HVm; webwx_data_ticket=gSevzn7SfyFmnjZpBE54BImF';
            $user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36';
            curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
            curl_setopt($ch, CURLOPT_COOKIE, $cookie);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLINFO_HEADER_OUT, true);
            $output = curl_exec($ch);
            // $request_header = curl_getinfo($ch,CURLINFO_HEADER_OUT);
            curl_close($ch);

            // dump($request_header);
            $output_arr = json_decode($output, true);
            if (strpos($output_arr['FullURL'], 'weixin110.qq.com')) {
                continue;
            } else {
                // header('HTTP/1.1 301 Moved Permanently');//发出301头部
                // header("Location:https://".$domain_list['domain'].$domain_list['postfix']);//跳转到你希望的地址格式
                return json(['status' => 1, 'url' => $domain_list['domain'].'/'.$domain_list['postfix']]);
            }
        }

        return json(['status' => 3, 'msg' => 'Unusable domain name']);
    }

    public function getCookie()
    {
        $url = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxstatreport?fun=new';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HTTPGET, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $host = array('Host: wx2.qq.com');
        $cookie = 'pgv_pvi=7363110912; RK=oOwB1gqw7/; tvfe_boss_uuid=1606ac0fae971cdb; luin=o1106678464; lskey=00010000cbe0981ebafc82e3a17cfcde0fadfb03d48212c7a1bd794c290740f65003a616f67b34e844d985e1; ptui_loginuin=2221691750; ptcz=049f9c758a08f1f251e4ff9c36627a101925d7e0eecf42c2d4e0cd58f2673829; pt2gguin=o2221691750; pac_uid=1_1106678464; o_cookie=1106678464; pgv_pvid=367289993; uid=848493667; webwxuvid=063b75eb8f7916daf2824c0c1d463ef40a48af81ff68505020c8fe767b9d9eeef1f12bdc9c20e48ad138b93ec5970ee1; login_frequency=1; last_wxuin=455750360; webwx_auth_ticket=CIsBELCiyJcPGoABPKZij0SS+cKOrT6DvVpLuoQPQ8We9reIoQBKyjicNV8RsoURqQKPR+MgTJxo/KhhG3fOUDVMsdajTYapsIbt7R4ixbtdp/sSbWow5aOJJTjdsloxesamUJuh75Hui5/qm2Mfu37PYIGmnBo+DVWbwMLKarLGvD+WtLds9gde1i4=; wxloadtime=1536119938_expired; mm_lang=zh_CN; MM_WX_NOTIFY_STATE=1; MM_WX_SOUND_STATE=1; wxpluginkey=1536101882; wxuin=548327195; wxsid=yH0N/B6M0f/nTKpt; webwx_data_ticket=gSf9rQ15M+8nsrHT4nLZCGVJ';
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $host);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        $output = curl_exec($ch);
        $request_header = curl_getinfo($ch, CURLINFO_HEADER_OUT);
        curl_close($ch);
        // dump($request_header);
        print_r($output);
    }
}
